﻿using HttpClientHelper;
using KinTN_WMS_CL.DialogView;
using KinTN_WMS_CL.Model;
using KinTN_WMS_CL.Resource;
using KinTN_WMS_CL.Tools;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;

namespace KinTN_WMS_CL.UserControlModels
{
    public class MaterialStorageViewModel : BindableBase, INavigationAware
    {
        public ObservableCollection<ButtonViewModel> Buttons { get; private set; }

        private readonly IEventAggregator _eventAggregator;
        private readonly IDialogService _dialogService;

        public event Action<IDialogResult> RequestClose;
        private readonly IHttpClients _httpClients;
        public MaterialStorageViewModel(IDialogService dialogService, IEventAggregator eventAggregator, IHttpClients httpClients)
        {
            Buttons = new ObservableCollection<ButtonViewModel>(); // 初始化集合
            _dialogService = dialogService;
            _eventAggregator = eventAggregator;
            OpenChildViewCommand = new DelegateCommand(OpenChildView);
            ExportComm = new DelegateCommand(OnExportAsync);
            SelectComm = new DelegateCommand(ExecuteQuery);
            _httpClients = httpClients;
            GenerateButtons();
        }

        public ICommand OpenChildViewCommand { get; }

        public ICommand ExportComm { get; }

        public ICommand SelectComm { get; }

        private string _inputText;

        public string InputText
        {
            get => _inputText;
            set
            {
                if (_inputText != value)
                {
                    _inputText = value;
                    SetProperty(ref _inputText, value);
                }
            }
        }

        public string Title => throw new NotImplementedException();

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {

        }

        private void OpenChildView()
        {
            //向开启的界面传参
            DialogParameters keys = new DialogParameters();
            keys.Add("Title", "新增");
            _dialogService.ShowDialog(nameof(MaterialStorageDetailsView), keys, callback =>
            {
                var Result = callback.Parameters.GetValue<DetailedShowModel>("ResultModel");
                if (callback.Result == ButtonResult.OK)
                {


                }
                else
                {
                    Result = callback.Parameters.GetValue<DetailedShowModel>("ResultModel");
                }
            });

        }


        private async void OnExportAsync()
        {
            await Task.Run(async () =>
            {
                var list = await _httpClients.PostAsync<ResponseModel<byte[]>>($"http://localhost:5032/api/Material/export", "", null);

                // 使用 SaveFileDialog 让用户选择保存位置
                SaveFileDialog saveFileDialog = new SaveFileDialog
                {
                    Filter = "Excel Files (*.xlsx)|*.xlsx",
                    FileName = "物料.xlsx"
                };

                if (saveFileDialog.ShowDialog() == true)
                {
                    // 将文件保存到指定位置
                    System.IO.File.WriteAllBytes(saveFileDialog.FileName, list.Data);
                    _eventAggregator.GetEvent<NotificationEvent>().Publish(new Message()
                    {
                        info = "导出成功",
                        type = MessageEnum.Success,
                    });
                }
            });
        }

        private void GenerateButtons()
        {
            Buttons.Add(new ButtonViewModel(ButtonType.add, "新增", "\\Assets\\Images\\add.png", new DelegateCommand(ExecuteAdd)));
            Buttons.Add(new ButtonViewModel(ButtonType.edit, "修改", "\\Assets\\Images\\edit.png", new DelegateCommand(ExecuteEdit)));
            Buttons.Add(new ButtonViewModel(ButtonType.delete, "删除", "\\Assets\\Images\\delete.png", new DelegateCommand(ExecuteDelete)));
            Buttons.Add(new ButtonViewModel(ButtonType.export, "导出", "\\Assets\\Images\\export.png", new DelegateCommand(ExecuteExport)));
            Buttons.Add(new ButtonViewModel(ButtonType.import, "导入", "\\Assets\\Images\\import.png", new DelegateCommand(ExecuteImport)));
        }

        private void ExecuteAdd()
        {
            // 添加逻辑
        }

        private void ExecuteEdit()
        {
            // 修改逻辑
        }

        private void ExecuteDelete()
        {
            // 删除逻辑
        }

        private void ExecuteExport()
        {
            // 导出逻辑
        }

        private void ExecuteImport()
        {
            // 导入逻辑
        }

        private void ExecuteQuery()
        {

            _eventAggregator.GetEvent<NotificationEvent>().Publish(new Message()
            {
                info = $"{InputText}查询成功！",
                type = MessageEnum.Warning,
            });
        }
    }
}
